package com.maozzi.web.controller;

import com.maozzi.api.es.EsService;
import com.maozzi.api.es.model.EsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author maozi
 */
@Slf4j
@RestController
public class IndexController {

	@Resource
	private EsService esService;

	@GetMapping("/")
	public String index() {
		log.info("index 首页");
		return "index";
	}

	@GetMapping("search")
	public String search(@RequestParam(required = false) String keyword) {
		log.info("search 搜索");
		List<EsDTO> list = esService.search(keyword);
		List<String> stringList = list.stream().map(EsDTO::toString).collect(Collectors.toList());
		return String.join(" , ", stringList);
	}
}
